<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class News_management extends MX_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model(array('admin_commoncall','admin_settings_model','admin_review_model','admin_news_model','admin_counter_model'));
		$this->load->library('pagination');
		//$this->admin_commoncall->check_admin_login();
		$this->template->set_theme('admin');
		$this->template->set_layout('default');
		//$this->template->set_partial('side_menu','layouts/side_menu');
		//$this->template->set_partial('nav_bar','layouts/nav_bar');

	}
	
	function index()
	{
		$offset = 0 + $this->uri->segment(5);
		$config['base_url'] = base_url() . 'secure/news/news_management/index';
		$total = $this->admin_counter_model->count_all_record('news');
		$perpage = 3;
		$config['per_page'] = $perpage;
		$config['total_rows'] = $total;
		$config['uri_segment'] = '5';
		
		$data['news']['all'] = $this->admin_news_model->get_all_news($perpage, $offset);
		$this->pagination->initialize($config);
		
		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')
		//->set_partial('footer', 'footer/footer')
		->build('news/news_management',$data);
	}
	
	function submit_news() {
	
	
		$this->form_validation->set_rules('title', 'Title', 'trim|required|xss_clean');
		$this->form_validation->set_rules('meta_title', 'Meta Title', 'trim|required|xss_clean');
		$this->form_validation->set_rules('meta_keyword', 'Meta Keyword', 'trim|required|xss_clean');
		$this->form_validation->set_rules('meta_description', 'Meta Description', 'trim|required|xss_clean');
		$this->form_validation->set_rules('news_detail', 'Description', 'trim|required|xss_clean');
		
		if ($this->form_validation->run() == true) {
	
	
			$data = array(
					
					'title' => $this->input->post('title'),
					'meta_title' => $this->input->post('meta_title'),
					'meta_keyword' => $this->input->post('meta_keyword'),
					'meta_description' => $this->input->post('meta_description'),
					'description' => $this->input->post('news_detail'),
			);
	
			//var_dump($data);
			//die();
			
			$this->db->insert('news',$data);
			$msg = "Your Data Post Successfully";
			$this->session->set_flashdata('message', $msg);
			redirect('secure/news/news_management');
			exit();
		}else{$msg = "Fields can't blank !";
		$this->session->set_flashdata('message', $msg);
		redirect('secure/news/news_management');
		exit();
		}
	
	}
	function edit_news($id) {
	
		$data['newsrow']['info'] = $this->admin_news_model->get_news($id);
		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')
		->build('news/edit_news', $data);
	}
	function update_news(){
		
	
	$this->form_validation->set_rules('title', 'Title', 'trim|required|xss_clean');
	$this->form_validation->set_rules('meta_title', 'Meta Title', 'trim|required|xss_clean');
	$this->form_validation->set_rules('meta_keyword', 'Meta Keyword', 'trim|required|xss_clean');
	$this->form_validation->set_rules('meta_description', 'Meta Description', 'trim|required|xss_clean');
	$this->form_validation->set_rules('news_detail', 'Description', 'trim|required|xss_clean');
	$this->form_validation->set_rules('news_id', 'Required', 'trim|required|');
	
	$id = $this->input->post('news_id');
	if ($this->form_validation->run() == true) {
	
	
		$data = array(
				
				'id' => $this->input->post('news_id'),
				'title' => $this->input->post('title'),
				'meta_title' => $this->input->post('meta_title'),
				'meta_keyword' => $this->input->post('meta_keyword'),
				'meta_description' => $this->input->post('meta_description'),
				'description' => $this->input->post('news_detail'),
				'posted_on' =>date('d-M-Y',time())
		);
	
		//var_dump($data);
		//die();
		//$this->db->where('id', $id);
		$this->db->where('id', $id);
		$this->db->update('news',$data);
		
		$this->session->set_flashdata('message', 'Updated successfully..');
		redirect('secure/news/news_management');
		exit();
	}else{$msg = "Fields can't blank !";
	$this->session->set_flashdata('message', $msg);
	redirect('secure/news/news_management');
	exit();
	}
}
function delete_news($id)
{
	$this->admin_news_model->delete_news($id);
	$this->session->set_flashdata('msg', 'News Deleted...');
	redirect('secure/news/news_management');
}

}
